package com.example.mark.controller;

import com.example.mark.common.Result;
import com.example.mark.entity.CollegeMark;
import com.example.mark.entity.Mark;
import com.example.mark.mapper.CollegeMapper;
import com.example.mark.service.Impl.CollegeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class CollegeController {
    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private CollegeServiceImpl collegeServiceImpl;
    /**
     * 首页请求响应 页数从1开始
     * 排序从综合总分依次向下递减
     * @param {"pageNum":int}
     * @return result
     * */
    @PostMapping("/selete10college")
    public Result select(@RequestBody Map<String, Integer> requestParams) {
        int pageNum = requestParams.get("pageNum");

        int limt = 10;
        int offset = 0;
        if (pageNum != 1){
            offset = (pageNum-2)*10 + 9;
        }

        List<CollegeMark> list = collegeMapper.selectComprehensiveMarkWithCollege(limt, offset);
        return Result.success(list);
    }

    /**
     * 首页模糊查询请求响应 页数从1开始
     * 排序从综合总分依次向下递减
     * @param {"pageNum":1,"collegeName":"韩"}
     * @return result
     * */
    @PostMapping("/selete10collegeLike")
    public Result selectLike(@RequestBody Map<String, Object> requestParams) {
        // 从请求参数中获取页码和高校名称
        Integer pageNum = (Integer) requestParams.get("pageNum");
        String collegeName = (String) requestParams.get("collegeName");

        // 检查高校名称是否为空
        if (collegeName == null || collegeName.isEmpty()) {
            // 返回错误信息，例如"高校名称不能为空"
            return Result.error("高校名称不能为空");
        }

        int limit = 10;
        int offset = 0;

        // 计算offset，第一页的offset为0，第二页为10，以此类推
        if (pageNum != null && pageNum > 1) {
            offset = (pageNum - 1) * limit;
        }

        // 调用collegeMapper的方法获取数据
        List<CollegeMark> list = collegeMapper.selectComprehensiveMarkWithCollegeLike(limit, offset, collegeName);

        // 返回成功结果
        return Result.success(list);
    }

    /**
     * 详情页请求1 请求高校详细内容
     * @param {"collegeId":4144010558,"sort":1}
     * @return 数据返回
     * */
    @PostMapping("/collegeSort")
    public Result selectCollegeSort(@RequestBody Map<String, Object> requestParams){

        Object collegeIdObj = requestParams.get("collegeId");
        if (collegeIdObj instanceof String) {
            BigInteger collegeId = new BigInteger((String) collegeIdObj);
            int sort = (int) requestParams.get("sort");

            Result result = collegeServiceImpl.selectCollegeSort(collegeId, sort);

            return result;
        } else {
            // 如果collegeId不是String类型，可以在这里添加错误处理逻辑
            // 例如返回一个错误响应
            return Result.error("Invalid collegeId type");
        }

    }
    /**
     * 点赞功能
     * */
    @PostMapping("/upvote")
    public Result upvote(@RequestBody Map<String,Object> requestParams){
        int collegeSortId = (int) requestParams.get("id");
        if(collegeServiceImpl.upvote(collegeSortId)){
            return Result.success("true");
        }else {
            return Result.error("false");
        }
    }

    @PostMapping("/mark")
    public Result mark(@RequestBody Mark mark){
        collegeServiceImpl.mark(mark);
        return collegeServiceImpl.mark(mark);
    }
}
